package top.milkbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.milkbox.domain.Userinfo;
import top.milkbox.service.UserinfoService;
import top.milkbox.mapper.UserinfoMapper;
import org.springframework.stereotype.Service;
import top.milkbox.utils.GuoHttpUtils;
import top.milkbox.utils.R;

import javax.servlet.http.HttpSession;

/**
 * @author xiaoguo
 * @description 针对表【tb_userinfo】的数据库操作Service实现
 * @createDate 2022-04-26 10:47:19
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
        implements UserinfoService {

    @Autowired
    private GuoHttpUtils guoHttpUtils;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Override
    public HttpSession login(String code, HttpSession session) {
        // 获取session
        Userinfo userinfoSession = (Userinfo) session.getAttribute("userinfo");
        System.out.println("------> code: " + code);
        System.out.println("------> userinfoSession: " + userinfoSession);

        // 判断是否有用户的信息
        if (userinfoSession != null) {
            // 用户已经登录
            System.out.println("------> 用户已经登录");
            return session;
        } else {
            //  用户未登录或者用户未注册

            // 发送http请求，通过code从微信服务器获取用户的openid
            GuoHttpUtils.WeChatInfo weChatInfo = guoHttpUtils.getOpenidAndSessionKey(code);
            System.out.println("------> weChatInfo: " + weChatInfo);

            // 通过openid查询用户信息，判断用户存在
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("openid", weChatInfo.getOpenid());
            Userinfo userinfo = userinfoMapper.selectOne(queryWrapper);
            System.out.println("------> userinfo: " + userinfo);

            if (userinfo == null) {
                // 用户未注册
                System.out.println("------> 用户未注册");
                return null;
            } else {
                // 用户已经注册，将用户信息放入session
                System.out.println("------> 用户已经注册");
                userinfo.setSessionKey(""); // 去除session_key信息
                session.setAttribute("userinfo", userinfo);
                Userinfo userinfoSession2 = (Userinfo) session.getAttribute("userinfo");
                System.out.println("------> userinfoSession2: " + userinfoSession2);
                return session;
            }

        }
    }

    @Override
    public boolean register(String code, Userinfo userinfo) {

        if (userinfo != null && code != null) {
            // 发送http请求，通过code从微信服务器获取用户的openid
            GuoHttpUtils.WeChatInfo weChatInfo = guoHttpUtils.getOpenidAndSessionKey(code);
            userinfo.setOpenid(weChatInfo.getOpenid());
            userinfo.setSessionKey(weChatInfo.getSession_key());
            userinfo.setDeleted(0);
            System.out.println("------> userinfo: " + userinfo);
            // 向数据库插入用户信息
            int affect = 0;
            try {
                affect = userinfoMapper.insert(userinfo);
            } catch (Exception e) {
                System.out.println("------> 数据已存在");
            }
            return affect == 1;
        } else {
            return false;
        }
    }

    @Override
    public Boolean logout(HttpSession session) {
        System.out.println("------> session: " + session.getId());
        if (session != null) {
            Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
            if (userinfo != null) {
                session.invalidate(); // 销毁session
                System.out.println("------> session销毁成功");
                return true;
            }
        }
        System.out.println("------> 无此session，登出失败");
        return false;

    }
}




